package com.mdlib.helper.module.account.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ljlib.core.util.EmptyUtils;
import com.ljlib.core.util.LogUtil;
import com.ljlib.help.R;
import com.mdlib.helper.base.BaseAuthFragment;
import com.mdlib.helper.common.MDConstant;
import com.mdlib.helper.event.LoginEvent;
import com.mdlib.helper.model.AccountModel;
import com.mdlib.helper.model.entity.AuthUser;
import com.mdlib.helper.presenter.LoginHelper;
import com.mdlib.helper.ui.UIHelper;
import com.mdlib.helper.utils.MDAppUtils;
import com.mdlib.helper.utils.ToastUtil;
import com.umeng.message.PushAgent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by librabin on 16/11/16.
 */

public class LoginFragment extends BaseAuthFragment {


    @Bind(R.id.et_login_name)
    EditText mEtLoginName;
    @Bind(R.id.et_login_pass)
    EditText mEtLoginPass;
    @Bind(R.id.tv_login_regist)
    TextView mTvLoginRegist;
    @Bind(R.id.tv_login_forget)
    TextView mTvLoginForget;
    @Bind(R.id.tv_login)
    TextView mTvLogin;
    @Bind(R.id.tv_login_sms)
    TextView mTvLoginSms;
    @Bind(R.id.tv_login_wechat)
    TextView mTvLoginWechat;
    @Bind(R.id.tv_login_back)
    TextView mTvLoginBack;
    @Bind(R.id.rl_login_third)
    RelativeLayout mRlLoginThird;
    @Bind(R.id.tv_sms_login)
    TextView mTvSmsLogin;


    public static LoginFragment newInstance() {
        return new LoginFragment();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        ButterKnife.bind(this, super.onCreateView(inflater, container, savedInstanceState));
        init();
        return super.onCreateView(inflater, container, savedInstanceState);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        ButterKnife.unbind(this);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_login;
    }

    private void init() {
        String cid = PushAgent.getInstance(getActivity()).getRegistrationId();
        LogUtil.e("Token  " + cid);

        if (AccountModel.getInstance().getRoleType().equals("user")) {
            mRlLoginThird.setVisibility(View.VISIBLE);
            mTvSmsLogin.setVisibility(View.GONE);
        } else if (AccountModel.getInstance().getRoleType().equals("run")) {
            mRlLoginThird.setVisibility(View.GONE);
            mTvSmsLogin.setVisibility(View.VISIBLE);
        }

    }


    @OnClick({R.id.tv_login_regist, R.id.tv_login_forget, R.id.tv_login, R.id.tv_login_sms, R.id.tv_login_wechat, R.id.tv_login_back, R.id.tv_sms_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_login_regist:
//                UIHelper.showLoginRelation(getActivity(), ContentType.REGIST);
                addFragment(RegisterFragment.newInstance());
                break;
            case R.id.tv_login_forget:
                UIHelper.showForgetPwdPage(getActivity());
                break;
            case R.id.tv_login:
                String loginName = mEtLoginName.getText().toString();
                String loginPwd = mEtLoginPass.getText().toString();
                if (EmptyUtils.isEmpty(loginName)) {
                    ToastUtil.showToast("用户名不能为空");
                    return;
                }
                if (!MDAppUtils.checkPassword(loginPwd)) {
                    return;
                }
                startLoadingDialog(false);
                LoginHelper.doLogin(loginName, loginPwd);
                break;
            case R.id.tv_login_sms:
                addFragment(SmsLoginFragment.newInstance());
                break;
            case R.id.tv_sms_login:
                addFragment(SmsLoginFragment.newInstance());
                break;
            case R.id.tv_login_wechat:
                startLoadingDialog(false);
                doWXAuth();
                break;
            case R.id.tv_login_back:
                removeFragment();
                break;
        }
    }


    @Override
    protected void featchAutherSucc(AuthUser authUser) {
        LoginHelper.doAuthLogin(authUser);
    }

    @Override
    protected void featchAutherFail(String msg) {
        startLoadingDialog(false);
        ToastUtil.showToast(msg);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(LoginEvent event) {
        stopLoadingDialog();
        if (event.code == MDConstant.STATE_SUCC) {
            ToastUtil.showToast("登录成功");
            AccountModel.getInstance().setLogin(true);
            UIHelper.showMainPage(getActivity());
        } else {
            ToastUtil.showToast(event.msg);
        }
    }


}
